<?php
class AppSmsKuteController extends CpController
{
    public function actionIndex()
    {
        $currentPage = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
        $currentPage = $currentPage==0 ? 1 : $currentPage;
        $numberRecordPerPage = Yii::app()->params["numberRecordPerPage"];
        
        $search = array();
        $search["toDate"] = isset($_GET["toDate"]) ? $_GET["toDate"] : "";
        $search["fromDate"] = isset($_GET["fromDate"]) ? $_GET["fromDate"] : "";
        $search["keyword"] = isset($_GET["keyword"]) ? trim(strip_tags($_GET["keyword"])) : "";
        $search["keyword_field"] = isset($_GET["keyword_field"]) ? trim(strip_tags($_GET["keyword_field"])) : "";
        $search["price"] = isset($_GET["price"]) ? trim(strip_tags($_GET["price"])) : "";
        $search["status"] = isset($_GET["status"]) ? trim(strip_tags($_GET["status"])) : "";
        
        $apps = AAppSmsKute::getAllApp($search, $currentPage, $numberRecordPerPage);
        $total_app = AAppSmsKute::getTotalApp($search);
        $numberPage = ceil($total_app / $numberRecordPerPage);
        
        $this->render(
            "index"
            , array(
                "search"=>$search
                , "apps"=>$apps
                , "total_app"=>$total_app
                , "currentPage"=>$currentPage
                , "numberPage"=>$numberPage
            )
        );  
    }
    
    public function actionSms()
    {
        $appId = isset($_GET["id"]) ? intval($_GET["id"]) : 0;
        
        /* Lấy thông tin app */
        $app = AAppSmsKute::getAppInfo($appId);
        if(empty($app)){
            throw new CHttpException(404, "Trang yêu cầu không tồn tại");
        }
        
        $sms = AAppSmsKute::getSms($app["list_sms_id"]);
        
        $this->render(
            "sms"
            , array(
                "app"=>$app
                , "sms"=>$sms                
            )
        );  
    }
    
    public function actionAjaxDelete()
    {        
        $id = isset($_POST["id"]) ? intval($_POST["id"]) : 0;
        if($id >0){
            $result = AAppSmsKute::DeleteApp($id);
            if($result >0){
                $act = 3;
                $obj = 24;
                $content = LogConfig::$action[$act] . " " . LogConfig::$object[$obj] . ": {AppId: " . $id . "}";
                $result = AAdminLog::insertAdminLog($act, $obj, $content);                  
                echo 1;exit();
            } else {
                echo "Chưa thay đổi dữ liệu";
            }
        }
    }    
}
?>
